﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;



public class datUser
{
    SqlCommand cmd;
    SqlDataReader dr;
    public List<entUser> getUserDangNhap(entUser sv)
    {
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr3"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);
        sqlConnection.Open();
        cmd = new SqlCommand("sp_UserDangNhap", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter[] sqla = new SqlParameter[2];
        sqla[0] = new SqlParameter("username", DbType.String);
        sqla[1] = new SqlParameter("pass", DbType.String);

        sqla[0].Value = sv.UserName;
        sqla[1].Value = sv.Pass;
        cmd.Parameters.AddRange(sqla);

        dr = cmd.ExecuteReader();
        entUser entsv = new entUser();
        List<entUser> lst = new List<entUser>();
        if (dr.Read())
        {
            entsv = new entUser();
            entsv.UserName = dr["username"].ToString();
            entsv.Userid = int.Parse(dr["userid"].ToString());
            entsv.Quyenid = int.Parse(dr["quyenid"].ToString());
            entsv.Khoa = dr["khoa"].ToString();
            lst.Add(entsv);
        }
        dr.Close();
        sqlConnection.Close();
        return lst;
    }
    public void  updateMatKhau(entUser sv)
    {
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr3"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);
        sqlConnection.Open();
        cmd = new SqlCommand("sp_UserUpdateMatKhau", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter[] sqla = new SqlParameter[2];
        sqla[0] = new SqlParameter("userid", DbType.Int16);
        sqla[1] = new SqlParameter("pass", DbType.String);

        sqla[0].Value = sv.Userid;
        sqla[1].Value = sv.Pass;
        cmd.Parameters.AddRange(sqla);

        cmd.ExecuteNonQuery();
        sqlConnection.Close();
       
    }
    public entUser getByUserId(int userid)
    {

        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr3"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);
        sqlConnection.Open();
        cmd = new SqlCommand("sp_UserGetByUserId", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter[] sqla = new SqlParameter[1];
        sqla[0] = new SqlParameter("userid", DbType.Int32);
        
        sqla[0].Value = userid;

        cmd.Parameters.AddRange(sqla);

        dr = cmd.ExecuteReader();
        entUser entU = new entUser();
        if (dr.Read())
        {
            entU.Userid = int.Parse(dr["userid"].ToString());
            entU.UserName = dr["username"].ToString();
            entU.Pass = dr["pass"].ToString();
        }
        dr.Close();
        sqlConnection.Close();
        return entU;
    }

    public List< entUser> getUserNhanGopY(int NhomUserId,string maKhoa)
    {

        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr3"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);
        sqlConnection.Open();
        cmd = new SqlCommand("sp_UserGetNguoiNhanGopYKien", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter[] sqla = new SqlParameter[2];
        sqla[0] = new SqlParameter("@NhomUserId", DbType.Int32);
        sqla[1] = new SqlParameter("@makhoa", DbType.String);

        sqla[0].Value = NhomUserId;
        sqla[1].Value = maKhoa;

        cmd.Parameters.AddRange(sqla);

        dr = cmd.ExecuteReader();
        entUser entU = new entUser();
        List<entUser> lst = new List<entUser>();
        while (dr.Read())
        {
            entU = new entUser();
            entU.Userid = int.Parse(dr["userid"].ToString());
            entU.UserName = dr["username"].ToString();
            entU.Pass = dr["pass"].ToString();
            entU.Email = dr["email"].ToString();
            lst.Add(entU);
        }
        dr.Close();
        sqlConnection.Close();
        return lst;
    }
}


